package com.example.android.android.demo

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.Role.Companion.Button
import androidx.compose.ui.tooling.preview.Preview
import com.example.android.android.demo.ui.theme.AndroidDemoTheme
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.FlutterView

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AndroidDemoTheme {
                Scaffold(modifier = Modifier.fillMaxSize(), floatingActionButton = {
                    Text(text = "@")
                }) { innerPadding ->
                    Greeting(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )

                }
            }
        }
    }


    fun openActivity() {
        val intent =
            FlutterActivity.withNewEngine().initialRoute("android 启动 flutterActivity")
                .build(this)
        startActivity(intent)

    }

    fun openFragmentActivity() {
        startActivity(Intent(this, FragmentActivity::class.java))
    }

    fun openViewActivity() {
        startActivity(Intent(this, ViewActivity::class.java))
    }

}



@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    val context = LocalContext.current
    Column {
        Button(onClick = {
            (context as MainActivity).openActivity()

        }){
            Text(
                text = "Hello $name!, 打开新的activity",
                modifier = modifier,
            )
        }
        Button(onClick = {
            (context as MainActivity).openFragmentActivity()

        }){
            Text(
                text = "打开新的FragmentActivity",
                modifier = modifier,
            )
        }
        Button(onClick = {
            (context as MainActivity).openViewActivity()

        }){
            Text(
                text = "打开新的FragmentActivity",
                modifier = modifier,
            )
        }

        //FlutterView(context)
    }

}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    AndroidDemoTheme {
        Greeting("Android")
    }
}